﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ras2Logic
{
    /// <summary>
    /// Klasinn SongController
    /// </summary>
    public class SongController
    {
        #region Member breytur

        /// <summary>
        /// Memberbreyta sem inniheldur tengingu við databaseinn.
        /// </summary>
        private Ras2DBDataContext m_context = new Ras2DBDataContext();

        #endregion

        #region Smiðir

        #endregion

        #region Get / Set föll

        #endregion

        #region Private föll

        #endregion

        #region Public föll

        /// <summary>
        /// Fall sem sækir öll lög.
        /// </summary>
        /// <returns>Skilar safni af lögum</returns>
        public IEnumerable<Song> GetAllSongs()
        {
            //Þetta skilar öllu úr Songs töfluni
            var result = from song in m_context.Songs
                         orderby song.Name ascending
                         select song;
            return result;
        }

        /// <summary>
        /// Fall sem sækir lög eftir flytjanda ID.
        /// </summary>
        /// <param name="nID">Tekur inn ID flytjanda</param>
        /// <returns>Skilar safni af lögum eftir tiltekinn flytjanda</returns>
        public IEnumerable<Song> GetSongsByPerformerID(int nID)
        {
            var result = from a in m_context.Songs
                         where a.PerformerID == nID
                         orderby a.Name ascending
                         select a;
            return result;
        }

        /// <summary>
        /// Fall sem sækir lög eftir ID lags.
        /// </summary>
        /// <param name="nID">Tekur inn ID lags</param>
        /// <returns>Skilar lagi af tegund Song</returns>
        public Song GetSongById(int nID)
        {
            return (from a in m_context.Songs
                    where a.ID == nID
                    select a).SingleOrDefault();
        }

        /// <summary>
        /// Fall sem uppfærir lag.
        /// </summary>
        /// <param name="s">Tekur inn lag af tegund Song</param>
        public void UpdateSong(Song s)
        {
            Song original = (from a in m_context.Songs
                                where a.ID == s.ID
                                select a).SingleOrDefault();

            if (original != null)
            {
                original.ID = s.ID;
                original.Name = s.Name;

                m_context.SubmitChanges();
            }
        }

        /// <summary>
        /// Fall sem bætir við nýju lagi.
        /// </summary>
        /// <param name="s">Tekur inn lag af tegund Song</param>
        public void AddSong(Song s)
        {
            m_context.Songs.InsertOnSubmit(s);
            m_context.SubmitChanges();
        }

        #endregion

    }
}
